﻿@using NewLife;
@using NewLife.Web;
@using XCode;
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;

    var act = Context.Request.Path + "";
    if (act.IsNullOrEmpty()) { act = Url.Action("Index"); }

    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;
}
<div class="tableTools-container">
    <div class="form-inline">
        <form action="@Html.Raw(page.GetFormAction(act))" method="post" role="form">
            @if (set.EnableAdd && !set.IsReadOnly && this.Has(PermissionFlags.Insert))
            {
                var rv = page.GetRouteValue();
                @Html.ActionLink("添加", "Add", rv, new { @class = "btn btn-success btn-sm" }.ToDictionary())
            }
            @if (set.EnableSelect)
            {
                <div class="form-group toolbar-batch">
                    @await Html.PartialAsync("_List_Toolbar_Batch")
                </div>
            }
            @await Html.PartialAsync("_List_Toolbar_Custom")
            <div class="pull-right form-group">
                @await Html.PartialAsync("_List_Search")
                @await Html.PartialAsync("_List_Toolbar_Search")
                @await Html.PartialAsync("_List_Toolbar_Adv")
            </div>
        </form>
    </div>
</div>
@if (set.EnableSelect)
{
    <script>
        $(function () {
            var $toolbarContext = $('.toolbar-batch'),
                $batchButtons = $('button[data-action="action"], input[data-action="action"]'), //button, input=button, a 3种类型都可以
                $table = $('.table'),
                $keys = $('input[name="keys"]', $table);

            $table.on('click', '#chkAll', function () {
                // 全选
                $keys.prop('checked', this.checked);
                // 启用禁用批量操作区
                $batchButtons.prop('disabled', !this.checked);
            });

            $table.on('click.checked', 'tbody input[name="keys"]', function (e) {
                //页面中所有的checkbox
                var allbox = $table.find('tbody :checkbox[name="keys"]');
                //页面中所选中的checkbox
                var selecteds = $table.find('tbody :checkbox:checked[name="keys"]');
                if (selecteds.length > 0) {
                    // 启用禁用批量操作区
                    $batchButtons.prop('disabled', false);
                    //需要判断当前页面所有行的checkbox是否都选中，以此来决定是否需要改变checkbox#chkAll 的状态
                    if (allbox.length == selecteds.length) {
                        $table.find('#chkAll').prop('checked', true);
                    } else {
                        $table.find('#chkAll').prop('checked', false);
                    }
                }
                else {
                    $batchButtons.prop('disabled', true);
                    $table.find('#chkAll').prop('checked', false);
                }
            });
        })
    </script>
}